package pri.damai.xiaowu.anydoor.netty;

import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import pri.damai.xiaowu.anydoor.business.BaseModel;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 处理器持有者
 *
 * @description
 * @author YangXinFu
 * @date 14:28 2022/7/25
 **/
@Slf4j
@Configuration
public class HandlerHolder {

    public static Map<String, SimpleChannelInboundHandler> handlerMap;

    public static Map<Byte, Class<? extends BaseModel>> dataModelMap = new HashMap<>();

    @Resource
    ApplicationContext applicationContext;

    @PostConstruct
    public void run() {
        handlerMap = applicationContext.getBeansOfType(SimpleChannelInboundHandler.class);

//        for (SimpleChannelInboundHandler<?> handler : handlerMap.values()) {
//
//            Type type = handler.getClass().getGenericSuperclass();
//            if (type instanceof ParameterizedType) {
//                ParameterizedType parameterizedType = (ParameterizedType) type;
//                Class<BaseModel> modelClass = (Class<BaseModel>) parameterizedType.getActualTypeArguments()[0];
//                BaseModel baseModel = modelClass.newInstance();
//                Byte code = baseModel.code();
//                dataModelMap.put(code, modelClass);
//                log.info("Code数据模型映射关系装载完成。{}", dataModelMap);
//            }
//        }

    }

    public static Class<? extends BaseModel> getModelClass(Byte code) {
        return dataModelMap.get(code);
    }
}
